package com.zhou;

import com.zhou.dao.StudentDao;
import com.zhou.domain.Student;
import com.zhou.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class MybatisTest2 {
    @Test
    public void t1()  {
        System.out.println("我来测试");
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //String sqlId = "com.zhou.dao.StudentDao.selectStudents";
        //List<Student> studentList = sqlSession.selectList(sqlId);
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        List<Student> students = mapper.selectStudents();
        for(Student stu : students){
            System.out.println("查询的学生="+stu);
        }
    }

    @Test
    public void testupdate2()  {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        Student student = new Student();
        student.setId(1);
        student.setAge(30);
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        int rows = mapper.updateStudent(student);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("修改记录的行数"+rows);
    }

    @Test
    public void testDelete2()  {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        int id =2;
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        int rows = mapper.deleteStudent(id);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("修改记录的行数"+rows);
    }

    @Test
    public void addStudentTest(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        Student student = new Student();
        student.setId(4);
        student.setAge(21);
        student.setEmail("1324189@qq.com");
        student.setName("zhou");
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        int i = mapper.insertOneStudent(student);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("一共增加了"+i+"数据");
    }

}
